// components/authorize/authorize.js
const app = getApp();
const host = app.globalData.host;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    visible: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
  },

  lifetimes: {
    attached: function () {
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getUserInfo(e) {
      var self = this;
      console.log(e);
      var userInfo = e.detail.userInfo;
      if (!userInfo) {
        wx.reLaunch({
          url: '/pages/index/index'
        })
        return;
      }
      var nickName = userInfo.nickName;
      var avatarUrl = userInfo.avatarUrl;
      var gender = userInfo.gender; //性别 0：未知、1：男、2：女
      var province = userInfo.province;
      var city = userInfo.city;
      var country = userInfo.country;
      wx.login({
        success: function (res) {
          var code = res.code;
          wx.request({
            url: host + '/yq/auth',
            data: {
              code: code,
              nickName: nickName,
              avatarUrl: avatarUrl,
              gender: gender,
              province: province,
              country: country
            },
            success: function (res) {
              console.log(res);
              var userInfo = res.data.info.user;
              var unionId = res.data.info.unionId;
              var openId = res.data.info.openId;
              var token = res.data.info.token;
              wx.setStorageSync("openId", openId);
              wx.setStorageSync("token", token);
              app.globalData.userInfo = userInfo;

              if (wx.getStorageSync("path")) {
                wx.reLaunch({
                  url: '/' + wx.getStorageSync("path")
                })
                wx.setStorageSync("path", null);
              } else {
                wx.reLaunch({
                  url: '/pages/index/index'
                })
              }

              // self.setData({
              //   visible: false
              // })
            }
          })
        },
        fail(errMsg) {
          console.log(errMsg)

          // self.setData({
          //   visible: false
          // })
        }
      })
    },
    cancel(e) {
      // this.setData({
      //   visible: false
      // })
      wx.reLaunch({
        url: '/pages/index/index'
      })
    }
  }
})
